/**
 * 
 */
package pl.kane.autokomp.applications;


import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


import pl.kane.autokomp.applications.dto.MenuData;
import pl.kane.autokomp.applications.dto.MenuItemData;
import pl.kane.autokomp.applications.dto.MenuSubItemData;
import pl.kane.autokomp.applications.exceptions.SystemException;
import pl.kane.autokomp.applications.utils.MenuUtilLoader;

/**
 * @author Lukasz Wator
 *
 */
public class MainMenu extends JMenuBar {
	
	private static final long serialVersionUID = 5839774671514064055L;

	public MainMenu() throws SystemException  {
		super();
		try {
			createMainMenu();
		} catch (InstantiationException e) {
			throw new SystemException("Exception while loading menu");
		} catch (IllegalAccessException e) {
			throw new SystemException("Exception while loading menu");
		} catch (ClassNotFoundException e) {
			throw new SystemException("Exception while loading menu");		}
	}
	
	protected void createMainMenu() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
	
		MenuData menu=MenuUtilLoader.getMenu();	
		
		for (int i=0;i<menu.getMenuItems().length;i++)
		{
			MenuItemData item = menu.getMenuItems()[i];
			JMenu jmenu = new JMenu(item.getName());
			for (int j=0;j<item.getSubItems().length;j++) {
				MenuSubItemData subMenu = item.getSubItems()[j];
				JMenuItem jMenuItem = new JMenuItem(subMenu.getName());
				jmenu.add(jMenuItem);
				if (subMenu.getActionPath()!=null && !subMenu.getActionPath().equals(""))
					jMenuItem.addActionListener((ActionListener)Class.forName(subMenu.getActionPath()).newInstance());
			}
			add(jmenu);
		}
	}
}
